<template>
	<view class="button-list e-flex e-flex-end" v-if="show">
		<template v-if="type === 'order'">
			<template v-if="isDFK">
				<!-- <button v-if="info.receivingStatus === 0 && info.orderPayType !== 'CASH'" @tap.stop="click(7)">{{ $t('order.立即付款') }}</button> -->
			</template>
			<template v-else>
				<button class="button-white e-m-r-20" v-if="info.receivingStatus === 0" @tap.stop="click(1)">{{ $t('order.取消订单') }}</button>
				<!-- <button class="button-white" v-if="info.receivingStatus === 5" @tap="click(2)">{{ $t('order.删除订单') }}</button> -->
				<!-- <button class="button-white e-m-l-20" v-if="info.receivingStatus === 5 && info.orderPayType !== 'OFFLINE'&&!info.deputys.some(item => !!item.formJson)" @tap.stop="click(6)">{{ $t('order.重新购买') }}</button> -->
				<!-- <button v-if="info.receivingStatus === 0 && info.orderPayType !== 'CASH'" @tap.stop="click(7)">{{ $t('order.立即付款') }}</button> -->
				<!-- <button v-if="isDetail && info.receivingStatus === 1"  @tap="click(1)">{{ $t('order.提醒发货') }}</button> -->
				<button v-if="refundBtnShow" @tap.stop="click(4)">{{ $t('order.退款') }}</button>
				<button v-if="info.receivingStatus === 6" @tap.stop="click(3)">{{ $t('order.取消退款') }}</button>
				<button v-if="info.receivingStatus === 2 && info.orderRefundStatus !== 2 && info.orderRefundStatus !== 1" @tap.stop="click(5)">{{ $t('order.确认收货') }}</button>
				<button v-if="info.receivingStatus === 10 && !isDetail" @tap.stop="click(8)">{{ $t('user.去使用') }}</button>
				<!-- <button v-if="info.orderRefundStatus === 2" @tap.stop="click(9)">查看详情</button> -->
			</template>
			
		</template>
		<template v-else>
			<button v-if="info.returnMoneySts === 0" @tap.stop="click(3)">{{ $t('order.取消退款') }}</button>
		</template>
	</view>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
	// #ifdef MP-WEIXIN
	// 将自定义节点设置成虚拟的，更加接近Vue组件的表现
	options: {
		virtualHost: true
	}
	// #endif
});
</script>

<script setup lang="ts">
import { computed } from 'vue';
import { useInit } from '@/common/stores';
import { storeToRefs } from 'pinia';
import { useCurrentInstance } from '@/common/hooks';
import { useI18n } from 'vue-i18n';
import { throttle } from '@/common/utils';
import type { OrderItem } from '@/common/request/api';

const props = withDefaults(
	defineProps<{
		info: OrderItem;
		type: string;
		isDetail: boolean;
		isDFK:{
			type:Boolean,
			default:false
		}
	}>(),
	{
		type: 'order'
	}
);

const emit = defineEmits(['cancel', 'refund', 'receiving', 'pay', 'deleteOrder', 'cancelRefund']);

const ctx = useCurrentInstance();
const { t } = useI18n();

const { isRefund } = storeToRefs(useInit());

const refundBtnShow = computed(() => {
	if (isRefund.value) {
		return (props.info.receivingStatus === 10 && props.isDetail) || (props.info.receivingStatus === 1 && props.info.orderRefundStatus === 0);
	}
	return false;
});

const show = computed(() => {
	if(props.type === 'order'){
		return [0,2,5,6].includes(props.info?.receivingStatus)||refundBtnShow.value||(props.info.receivingStatus === 10 && !props.isDetail)
	}
	return props.info?.returnMoneySts === 0
})



const click = (type: number) => {
	switch (type) {
		case 1:
			emit('cancel');
			break;
		case 2:
			deleteOrder();
			break;
		case 3:
			cancelRefund();
			break;
		case 4:
			emit('refund');
			break;
		case 5:
			emit('receiving');
			break;
		case 6:
			let params = {
				buys: []
			};

			props.info.deputys.forEach((item) => {
				params.buys.push({
					qty: item.commodityCount,
					sku: item.commoditySpecId
				});
			});
			// 配送改版 新增字段receivingType
			const deliveryMode = props.info.deliveryMode?.split(',');
			let _receivingType = 2;
			if (deliveryMode) {
				if (deliveryMode.includes('3')) {
					_receivingType = 3;
				} else if (deliveryMode.includes('6')) {
					_receivingType = 6;
				} else {
					_receivingType = deliveryMode[0];
				}
			}
			ctx.$goTo('/pages/systemPay/goods/previewOrder', { type: 3, receivingType: _receivingType, buys: JSON.stringify(params) });
			break;
		case 7:
			emit('pay');
			break;
		case 8:
			// 使用核销码
			ctx.$goTo('/pages/systemPay/order/details/index', { id: props.info.id });
			break;
			// case 9:
			// 	// 查看退款详情
			// 	ctx.$goTo(`/pages/order/refund/index`);
			// 	break;
	}
};
const deleteOrder = throttle(function () {
	ctx.$showModal({
		content: t('order.确定要删除订单吗'),
		success: (res) => {
			if (res.confirm) {
				emit('deleteOrder');
			}
		}
	});
}, 1000);
const cancelRefund = throttle(function () {
	ctx.$showModal({
		content: t('order.确定要取消退款吗'),
		success: (res) => {
			if (res.confirm) {
				emit('cancelRefund');
			}
		}
	});
}, 1000);
</script>

<style lang="scss" scoped>
.button-list {
	padding: 20rpx 30rpx;
	button {
		width: 172rpx;
		height: 70rpx;
		line-height: 70rpx;
		font-size: 30rpx;
		border-radius: 36rpx;
		border: 1px solid var(--color-primary);
		color: #fff;
		margin: 0;
		padding: 0;
		color:var(--color-primary);
		background: #fff;
	}
	.button-white {
		border: 1px solid #b4b4b4;
		color: #353535;
	}
}
</style>
